<template>
  <div class="user-profile-container">
    <el-card class="profile-card">
      <template #header>
        <div class="card-header">
          <h2>个人资料</h2>
          <el-button type="primary" @click="editMode = true" v-if="!editMode">编辑资料</el-button>
        </div>
      </template>

      <el-form :model="userForm" label-width="100px" v-if="editMode">
        <el-form-item label="头像">
          <el-upload
            class="avatar-uploader"
            action="/api/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="userForm.avatar" :src="userForm.avatar" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><User /></el-icon>
          </el-upload>
        </el-form-item>
        
        <el-form-item label="姓氏">
          <el-input v-model="userForm.lastName" />
        </el-form-item>
        
        <el-form-item label="名字">
          <el-input v-model="userForm.firstName" />
        </el-form-item>
        
        <el-form-item label="电子邮箱">
          <el-input v-model="userForm.email" disabled />
        </el-form-item>
        
        <el-form-item label="手机号码">
          <el-input v-model="userForm.phone" />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="saveProfile">保存</el-button>
          <el-button @click="cancelEdit">取消</el-button>
        </el-form-item>
      </el-form>

      <div class="profile-view" v-else>
        <div class="avatar-section">
          <img :src="user.avatar || defaultAvatar" class="avatar" />
        </div>
        
        <el-descriptions :column="1" border>
          <el-descriptions-item label="姓名">
            {{ fullName }}
          </el-descriptions-item>
          <el-descriptions-item label="电子邮箱">
            {{ user.email }}
          </el-descriptions-item>
          <el-descriptions-item label="手机号码">
            {{ user.phone || '未设置' }}
          </el-descriptions-item>
          <el-descriptions-item label="注册时间">
            {{ formatDate(user.createdAt) }}
          </el-descriptions-item>
        </el-descriptions>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus'
import { User } from '@element-plus/icons-vue'
import defaultAvatar from '@/assets/default-avatar.png'

const store = useStore()
const editMode = ref(false)
const userForm = ref({})

const user = computed(() => store.getters.userInfo)

const fullName = computed(() => {
  return [user.value.lastName, user.value.firstName].filter(Boolean).join(' ') || '未设置姓名'
})

onMounted(() => {
  resetForm()
})

const resetForm = () => {
  userForm.value = {
    ...user.value,
    password: ''
  }
}

const handleAvatarSuccess = (response) => {
  userForm.value.avatar = response.data.url
  ElMessage.success('头像上传成功')
}

const saveProfile = async () => {
  try {
    await store.dispatch('user/updateProfile', userForm.value)
    ElMessage.success('资料更新成功')
    editMode.value = false
  } catch (error) {
    ElMessage.error('更新失败: ' + (error.response?.data?.message || error.message))
  }
}

const cancelEdit = () => {
  resetForm()
  editMode.value = false
}

const formatDate = (dateStr) => {
  return new Date(dateStr).toLocaleString()
}
</script>

<style scoped>
.user-profile-container {
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.avatar-section {
  text-align: center;
  margin-bottom: 20px;
}

.avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  object-fit: cover;
}

.avatar-uploader {
  display: flex;
  justify-content: center;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
  border: 1px dashed #d9d9d9;
  border-radius: 50%;
  cursor: pointer;
}

.profile-view {
  padding: 0 20px;
}
</style>